package com.hw.core.system.service;

import com.hw.core.system.entity.NoteBook;
import com.hw.core.system.entity.User;
import com.hw.core.system.repository.NoteBookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)//全部方法只读事务
@CacheConfig(cacheNames = "noteBook")
public class NoteBookService {
    @Autowired
    private NoteBookRepository noteBookRepository;


    @Cacheable(key = "'list'")//缓存返回值，下次调用方法体不执行
    public List<NoteBook> findAllNoteBook() {
        return noteBookRepository.findAll();
    }

    @Cacheable(key = "'list1'")
    public List<NoteBook> findCanShowNoteBook() {
        return noteBookRepository.findNoteBookByStatusEquals( 1 );
    }

    @Cacheable(key = "#id+'obj'")
    public NoteBook findNoteBookById(Integer id) {
        return noteBookRepository.findOne( id );
    }


    @CacheEvict(allEntries = true)
    @Transactional
    public NoteBook add(NoteBook noteBook) {
        return noteBookRepository.save( noteBook );
    }

    @CacheEvict(allEntries = true)
    @Transactional
    public void updateNameAndStatus(Integer id ,String name,Integer status) {
        NoteBook one = noteBookRepository.findOne( id );
        one.setName( name );
        one.setStatus( status );
    }

    @CacheEvict(allEntries = true)
    @Transactional
    public void delete(Integer id) {
        noteBookRepository.delete( id );
    }


    @Cacheable(key = "#uid+'&'+#page+'&'+#name+'page'")
    public Page<NoteBook> findMyNoteBook(Integer uid, Integer page, Integer size, String name) {
        PageRequest pageRequest = new PageRequest( page - 1, size );
        if (name != null && name != "") {
            return noteBookRepository.findNoteBookByUserIs( uid, name, pageRequest );
        }else {
            return noteBookRepository.findNoteBookByUserIs( uid, pageRequest );
        }

    }
    @Cacheable(key = "#page+'&'+#name+'page'")
    public Page<NoteBook> findAllNoteBook(Integer page, Integer size, String name) {
        PageRequest pageRequest = new PageRequest( page - 1, size );
        if (name != null && name != "") {
            return noteBookRepository.findNoteBookByNameLike( "%"+name+"%",pageRequest );
        }else {
            return noteBookRepository.findAll(  pageRequest );
        }

    }
    @CacheEvict(allEntries = true)
    @Transactional
    public void updateText(Integer id, String text) {
        NoteBook one = noteBookRepository.findOne( id );
        one.setText( text );
    }
    @CacheEvict(allEntries = true)
    @Transactional
    public void updateImg(Integer id, String img) {
        NoteBook one = noteBookRepository.findOne( id );
        one.setImg( img );
    }
}
